package com.xique.door.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.request.buildHouse.HouseClaimPageRequest;
import com.xique.door.bean.request.buildHouse.HouseClaimVerifyRequest;
import com.xique.door.biz.service.IHouseClaimBizService;
import com.xique.door.constant.HouseClaimConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author shd
 * @create 2022/4/19 14:35
 */
@RestController
@RequestMapping("/houseClaim")
public class BuildHouseClaimController extends BaseController {

    @Autowired
    private IHouseClaimBizService houseClaimBizService;

    /**
     * 查询房屋认领审核表列表
     */
    @PreAuthorize(hasPermi = "door:claim:list")
    @GetMapping("/list")
    public AjaxResult list(HouseClaimPageRequest request) {
        return houseClaimBizService.getHouseClaimPage(request);
    }

    @PreAuthorize(hasPermi = "door:claim:query")
    @PostMapping("/verify")
    public AjaxResult verify(@RequestBody @Valid HouseClaimVerifyRequest request) {
        if (StringUtils.isNull(request.getId()) && CollectionUtil.isEmpty(request.getIds())) {
            return AjaxResult.error("请传入id信息");
        }
        // 根据status通过/拒绝
        if (StringUtils.isNotNull(request.getId())) {
            if (HouseClaimConstant.Status.PASS.equals(request.getStatus())) {
                return houseClaimBizService.passHouseClaim(request.getId());
            } else {
                return houseClaimBizService.rejectHouseClaim(request.getId());
            }
        }
        if (CollectionUtil.isNotEmpty(request.getIds())) {
            for (Long id : request.getIds()) {
                if (HouseClaimConstant.Status.PASS.equals(request.getStatus())) {
                    houseClaimBizService.passHouseClaim(id);
                } else {
                    houseClaimBizService.rejectHouseClaim(id);
                }
            }
            return AjaxResult.success();
        }
        return AjaxResult.success();
    }
}
